﻿using System;
using System.Configuration;
using System.ServiceModel;
using CtefCrmImport.Common.Builders;

namespace CtefCrmImport
{
    class Program
    {
        static public void Main(string[] args)
        {
            try
            {
                var op = ConfigurationManager.AppSettings["OperationName"];
                var filePath = ConfigurationManager.AppSettings["InputFilePath"];
                var headerRows = int.Parse(ConfigurationManager.AppSettings["HeaderRowCount"]);
                switch (op)
                {
                    case "ImportDonors":
                        //new ImportDonors().Run(filePath);
                        break;

                    case "ImportStudents":
                        //new ImportStudents().Run(filePath);
                        break;

                    case "ImportDonations":
                        //new ImportDonations(new DonationBuilder1()).Run(filePath, headerRows);
                        break;

                    case "ImportDonations2":
                        //new ImportDonations(new DonationBuilder2()).Run(filePath, headerRows);
                        break;

                    case "ImportEmailsOnly":
                        //new ImportEmailsOnly().Run(filePath);
                        break;

                    default:
                        Console.WriteLine("Invalid OperationName in config file.");
                        break;
                }
            }

            //<snippetCRUDOperations3>
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp);
                Console.WriteLine("Code: {0}", ex.Detail.ErrorCode);
                Console.WriteLine("Message: {0}", ex.Detail.Message);
                Console.WriteLine("Trace: {0}", ex.Detail.TraceText);
                Console.WriteLine("Inner Fault: {0}",
                    null == ex.Detail.InnerFault ? "Has Inner Fault" : "No Inner Fault");
            }
            catch (System.TimeoutException ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine("Message: {0}", ex.Message);
                Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
                Console.WriteLine("Inner Fault: {0}",
                    null == ex.InnerException.Message ? "Has Inner Fault" : "No Inner Fault");
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine(ex.Message);

                // Display the details of the inner exception.
                if (ex.InnerException != null)
                {
                    Console.WriteLine(ex.InnerException.Message);

                    FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
                        as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
                    if (fe != null)
                    {
                        Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
                        Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
                        Console.WriteLine("Message: {0}", fe.Detail.Message);
                        Console.WriteLine("Trace: {0}", fe.Detail.TraceText);
                        Console.WriteLine("Inner Fault: {0}",
                            null == fe.Detail.InnerFault ? "Has Inner Fault" : "No Inner Fault");
                    }
                }
            }
            //</snippetCRUDOperations3>
            // Additional exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
            // SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.

            finally
            {
                Console.WriteLine("Press <Enter> to exit.");
                Console.ReadLine();
            }
        }
    }
}
